import { prodConfig } from '../config.js';
import { Client } from 'ssh2';
async function connectSSh() {
  const conn = new Client()
  conn.on('ready', () => {
    console.log('Client :: ready');

    conn.sftp(async (err, sftp) => {
      if (err) throw err;
      conn.shell((err, stream) => {
        if (err) throw err;
        stream.on('close', () => {
          console.log('Stream :: close');
          conn.end();
        }).on('data', (data) => {
          console.log('OUTPUT: ' + data);
        });
        //先删除服务器原有的代码；再拷贝新的代码进去
        stream.end(`sudo rm -rf ${prodConfig.runPath}/*;sudo cp -r dist/* ${prodConfig.runPath}\n${prodConfig.password}\nexit\n`);
      });

    });


  }).connect({
    host: prodConfig.ip, // 服务器 IP
    port: prodConfig.port,
    username: prodConfig.username,
    password: prodConfig.password
  });
}
async function runTask() {
  await connectSSh() // 拷贝至服务器目录，即根目录
}
runTask()
